package com.cskaoyan.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileUploadUtil{


    static String url;

    public static String upLoad(MultipartFile file, String localPath) {
        if (file.isEmpty()) {
            return null;
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        //加一个日期，防止重复
        fileName = new SimpleDateFormat("yyyyMMddHHmmss").
                format(new Date()) + "_" + fileName;
        //合并前缀，localhost:8081/wx/storage/fetch/文件名
        String path = localPath + fileName;
        File dest = new File(path);//通过路径创建文件

            //上传文件
        try {
            file.transferTo(dest); //保存文件
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        url="http://localhost:8083/wx/storage/fetch/"+fileName;
        return url;
    }
}
